package com.clgk.wechatmall.common.excel;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import org.apache.log4j.Logger;

public class FilePathUtils
{
  private static Logger logger = Logger.getLogger(FilePathUtils.class);
  private static final String PROTOCOL_FILE = "file";
  
  public static URL locateFromClasspath(String resourceName)
    throws Exception
  {
    URL url = null;
    
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (loader != null)
    {
      url = loader.getResource(resourceName);
      if (url != null) {
        logger.debug("Loading configuration from the context classpath (" + resourceName + ")");
      }
    }
    if (url == null)
    {
      url = ClassLoader.getSystemResource(resourceName);
      if (url != null) {
        logger.debug("Loading configuration from the system classpath (" + resourceName + ")");
      }
    }
    return url;
  }
  
  public static File fileFromURL(URL url)
  {
    if ("file".equals(url.getProtocol())) {
      return new File(URLDecoder.decode(url.getPath()));
    }
    return null;
  }
  
  public static String filePathFromURL(URL url)
  {
    if ("file".equals(url.getProtocol())) {
      return URLDecoder.decode(url.getPath());
    }
    return null;
  }
}
